package org.speed.spring.core;

import org.speed.spring.core.aop.OrderService;
import org.speed.spring.core.aop.UserService;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.expression.StandardBeanExpressionResolver;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.google.inject.name.Names;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
    	listableBeanFactory();
    	
    	//applicationContext();
    }

	private static void applicationContext() {
		String location = "spring-bean.xml";
    	ClassPathXmlApplicationContext aContext = new ClassPathXmlApplicationContext();
    	aContext.setConfigLocation(location);
    	
    	//aContext.addBeanFactoryPostProcessor(new TestBeanFactoryPostProcessor());

    	aContext.refresh();
    	
    	//BeanDefinition worker = aContext.getBeanFactory().getBeanDefinition("worker");
    	//System.out.println(worker);
    	
    	//User user = aContext.getBean("user", User.class);
        //System.out.println(user);
    	
    	
    	//Student student = aContext.getBean("student", Student.class); 
    	//System.out.println(student);
    	
    	Worker worker = aContext.getBean("worker", Worker.class);    	
    	System.out.println(worker);
    	
    	//worker.test();
	}

	private static void listableBeanFactory() {
		//创建IOC容器
    	DefaultListableBeanFactory beanfactory = new DefaultListableBeanFactory();
    	
    	beanfactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanfactory.getBeanClassLoader()));
    	
    	//自定义的处理器
    	//TestInstantiationAwareBeanPostProcessor testInstantiationAwareBeanPostProcessor = new TestInstantiationAwareBeanPostProcessor();
    	//beanfactory.addBeanPostProcessor(testInstantiationAwareBeanPostProcessor);    	
    	//LogBeanPostProcessor logBeanPostProcessor = new LogBeanPostProcessor();
    	//beanfactory.addBeanPostProcessor(logBeanPostProcessor);
    	
    	//注解类型注入的处理器
    	//AutowiredAnnotationBeanPostProcessor autowiredAnnotationBeanPostProcessor = new AutowiredAnnotationBeanPostProcessor();
    	//autowiredAnnotationBeanPostProcessor.setBeanFactory(beanfactory);
    	//beanfactory.addBeanPostProcessor(autowiredAnnotationBeanPostProcessor);
    	
    	//AOP代理的处理器
    	//BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
    	//beanNameAutoProxyCreator.setBeanFactory(beanfactory);
    	//beanNameAutoProxyCreator.setBeanNames("proxy");
    	//beanNameAutoProxyCreator.setInterceptorNames("proxyOrderService");
    	//beanfactory.addBeanPostProcessor(beanNameAutoProxyCreator);
    	
    	
    	
		
		
    	//加载Bean资源
    	Resource res = new ClassPathResource("spring-bean.xml");
    	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanfactory);
    	reader.loadBeanDefinitions(res);
    	
    	//加载properties文件的处理器
    	PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
    	Resource locations = new ClassPathResource("jdbc.properties");
    	propertyPlaceholderConfigurer.setLocations(locations);
		propertyPlaceholderConfigurer.postProcessBeanFactory(beanfactory);
    	
		
    	
    	
    	//动态注册 beanDefinition
    	/**
    	BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(User.class);
    	beanDefinitionBuilder.addPropertyValue("name", "1");
    	beanDefinitionBuilder.addPropertyValue("age", "2");
    	BeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
		beanfactory.registerBeanDefinition("people", beanDefinition );
    	
		User user = beanfactory.getBean("people", User.class);
		System.out.println(user.toString() );

		user = beanfactory.getBean("user", User.class);
		System.out.println(user.toString() );**/
		
    	/**
    	StudentFactory studentFactory = beanfactory.getBean("&student", StudentFactory.class); 
    	System.out.println(studentFactory.getClass());

    	Student student = beanfactory.getBean("student", Student.class); 
    	System.out.println(student);
    	
    	String[] names = beanfactory.getBeanDefinitionNames();
    	for (String string : names) {
			System.out.println(string);
			
		}**/	
    	
    	//Student student = beanfactory.getBean("student", Student.class); 
    	//System.out.println(student.getClass());
		
		
    	//UserService user = beanfactory.getBean("proxyFactoryBean", UserService.class);
    	//System.out.println(user.getClass().getName());
    	//user.doPay("1", 12);
    	
		
    	BeanDefinition worker = beanfactory.getBeanDefinition("worker");
    	System.out.println(worker);
    	//Worker worker = beanfactory.getBean("worker", Worker.class);    	
    	//System.out.println(worker);
    	
    	
    	//beanfactory.addPropertyEditorRegistrar(registrar);
    	
    	//beanfactory.registerBeanDefinition(beanName, beanDefinition);
    	
    	//beanfactory.registerCustomEditor(requiredType, propertyEditorClass);
    	
    	
    	System.out.println( "Hello World!" );
	}
}
